package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sms.service.OpaSmsService;
import com.budwk.app.sqfw.dto.*;
import com.budwk.app.sqfw.enums.SqfwFormSummaryFieldEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.services.SqfwFormSummaryService;
import com.budwk.app.sqfw.vo.HouseSurveyFormPlaceVO;
import com.budwk.app.sqfw.vo.SqfwFormSummaryUnitInfoVO;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.exception.BaseException;
import com.budwk.starter.common.openapi.annotation.*;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ZY.Wang
 * @since 2023/7/17
 */
@IocBean
@At("/platform/sqfw/formSummary")
@ApiDefinition(tag = "表汇总")
@Slf4j
public class SqfwFormSummaryController {

    @Inject
    private SqfwFormSummaryService sqfwFormSummaryService;

    @Inject
    private SysUserService sysUserService;

    @Inject
    private OpaSmsService opaSmsService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据条件查询场所名称")
    @ApiResponses
    @SLog(tag = "根据条件查询场所信息", value = "${args[0]},${args[1]}")
    @SaCheckLogin
    public Result<?> listUnitPlaceInfo(@Param("unitId") String unitId, @Param("type") String type, @Param("unitName") String checkPlaceNameStr, @Param("returnKey") String returnKey) {
        //List<String> list = sqfwFormSummaryService.listUnitPlaceName(unitId, type, checkPlaceNameStr, returnKey);
        List<Map> list = sqfwFormSummaryService.listUnitPlaceName(unitId, type, checkPlaceNameStr, returnKey);
        return Result.success(list);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据条件查询场所名称")
    @ApiResponses
    @SLog(tag = "停业", value = "${args[0]},${args[1]}")
    @SaCheckLogin
    public Result<?> changeCloseDown(@Param("id") String id) {
        Sqfw_formSummary sqfwFormSummary = sqfwFormSummaryService.fetch(id);
        if(ObjectUtil.isEmpty(sqfwFormSummary)){
            throw new BaseException("id无法查询到关联数据");
        }
        sqfwFormSummary.setCheckStatus(SqfwFormSummaryFieldEnum.CHECK_STATUS_CLOSED.getCode());
        sqfwFormSummary.setUpdateDate(new Date());
        sqfwFormSummaryService.update(sqfwFormSummary);
        return Result.success();
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据场所名称获取最新场所信息")
    @ApiResponses
    @SLog(tag = "根据场所名称获取最新场所信息", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getUnitPlaceLatestInfo(@Param("unitName") String unitNameStr, @Param("unitId") String unitId) {
        SqfwFormSummaryUnitInfoVO vo = sqfwFormSummaryService.getUnitPlaceLatestInfo(unitNameStr, unitId);
        return Result.success(vo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据场所名称获取最新场所信息详情")
    @ApiResponses
    @SLog(tag = "根据场所名称获取最新场所信息详情", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getUnitPlaceLatestInfoCondition(@Param("placeName") String placeName, @Param("unitId") String unitId, @Param("conditionKey") String conditionKey) {
        SqfwFormSummaryUnitInfoVO vo = sqfwFormSummaryService.getUnitPlaceLatestInfo(conditionKey, placeName, unitId);
        return Result.success(vo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据场所名称获取最新场所信息")
    @ApiResponses
    @SLog(tag = "根据场所名称获取最新场所信息", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getUnitPlaceLatestInfoById(@Param("id") String id) {
        SqfwFormSummaryUnitInfoVO vo = sqfwFormSummaryService.getUnitPlaceLatestInfoById(id);
        return Result.success(vo);
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "更新表数据")
    @ApiResponses
    @SLog(tag = "更新表数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> update(@Param("..") Sqfw_formSummary sqfwFormSummary, HttpServletRequest req) {
        String userId = SecurityUtil.getUserId();
        Sys_user sysUser = sysUserService.fetch(userId);
        sqfwFormSummary.setUpdateBy(sysUser.getId());
        sqfwFormSummary.setUpdateDate(new Date());
        sqfwFormSummary.setUpdateUserName(sysUser.getUsername());
        sqfwFormSummary.setUpdateUserPhone(sysUser.getLoginname());

        sqfwFormSummaryService.updateIgnoreNull(sqfwFormSummary);

        sqfwFormSummaryService.retryGenerateFireHiddenPdf(userId, sqfwFormSummary.getSqfwFlowId());
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "获取详情")
    @ApiResponses
    @SLog(tag = "获取详情", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> detail(@Param("id") String id) {
        Sqfw_formSummary fetch = sqfwFormSummaryService.fetch(id);
        return Result.success(fetch);
    }

    @At
    @Ok("json")
    @GET
    @ApiOperation(name = "获取详情")
    @ApiResponses
    @SLog(tag = "获取详情", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getToSqfwId(@Param("sqfw_id") String sqfw_id, @Param("createDate") String createDate) {
        Sqfw_formSummary fetch = sqfwFormSummaryService.getToSqfwId(sqfw_id, createDate);
        return Result.success(fetch);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除表数据")
    @SLog(tag = "删除表数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        Sqfw_formSummary sqfwFormSummary = sqfwFormSummaryService.fetch(id);
        if (sqfwFormSummary == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        sqfwFormSummaryService.delete(id);
        req.setAttribute("_slog_msg", sqfwFormSummary.getId());
        return Result.success();
    }

    @At("/generateFireHiddenPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成消防隐患排查表PDF 所有单位通用")
    @SLog(tag = "生成消防隐患排查表PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateFireHiddenPdf(@Param("..") SqfwFormSummaryWithFlowDTO formSummaryWithFlow) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.generateFireHiddenPdf(userId, formSummaryWithFlow);
        return Result.success();
    }

    @At("/getLastCheck")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取上一次检查")
    @SLog(tag = "获取上一次检查", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getLastCheck(@Param("address") String address, @Param("name") String name) {
        Sqfw_formSummary formSummary = sqfwFormSummaryService.getLastCheck(address, name);
        if (formSummary == null) return Result.error("没有查询到上一次检查");
        return Result.success(formSummary);
    }

    @At("/retryGenerateFireHiddenPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "重新生成消防隐患排查表PDF")
    @SLog(tag = "重新生成消防隐患排查表PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> retryGenerateFireHiddenPdf(@Param("sqfwFlowId") String sqfwFlowId) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.retryGenerateFireHiddenPdf(userId, sqfwFlowId);
        return Result.success();
    }

    @At("/retryGenerateFireHiddenPdfs")
    @Ok("json")
    @POST
    @ApiOperation(name = "重新批量生成消防隐患排查表PDF")
    @SLog(tag = "重新批量生成消防隐患排查表PDF", value = "${args[0]}")
    //@SaCheckLogin
    public Result<?> retryGenerateFireHiddenPdfs(@Param("ids") String ids, @Param("isAll") Boolean isAll) {
        if (StringUtils.isNotBlank(ids)) {
            sqfwFormSummaryService.retryGenerateFireHiddenPdfs(Arrays.asList(ids.split(",")), isAll);
        } else {
            sqfwFormSummaryService.retryGenerateFireHiddenPdfs(null, isAll);
        }
        return Result.success();
    }

    @At("/retryGenerateFireHiddenPdfForAdmin")
    @Ok("json")
    @GET
    @ApiOperation(name = "重新生成消防隐患排查表PDF 用于临时生成")
    @SLog(tag = "重新生成消防隐患排查表PDF")
    public Result<?> retryGenerateFireHiddenPdfForAdmin(@Param("sqfwFlowId") String sqfwFlowId) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.retryGenerateFireHiddenPdf(userId, sqfwFlowId);
        return Result.success();
    }

    /**
     * 供小程序使用，无权限，只需要登陆即可
     *
     * @return 返回url
     */
    @At("/generateFireHiddenExcelForApp")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成消防隐患排查表Excel合集")
    @SLog(tag = "生成消防隐患排查表Excel", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateFireHiddenCollectionExcelForApp(@Param("sqfwId") String sqfwId, @Param("workOrderId") String workOrderId, @Param("fileIndex") Integer fileIndex) {
        String userId = SecurityUtil.getUserId();
        String excelUrl = sqfwFormSummaryService.generateFireHiddenCollectionExcelForApp(sqfwId, workOrderId, userId, fileIndex);
        if (StringUtils.isBlank(excelUrl)) return Result.error("暂无数据，不能生成Excel");
        return Result.data(excelUrl);
    }

    @At("/sendFireHiddenPeersMsg")
    @Ok("json")
    @POST
    @ApiOperation(name = "发送同行人员短信消息")
    @SLog(tag = "发送同行人员短信消息", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> sendFireHiddenPeersMsg(@Param("..") FireHiddenPeersMsgDTO dto) {
        int sendCount = sqfwFormSummaryService.sendFireHiddenPeersMsg(dto);
        if (sendCount <= 0) return Result.error("发送失败");
        return Result.success();
    }

    /**
     * 供小程序使用
     *
     * @return 返回url
     */
    @At("/generateFormSummaryPdfConverge")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成Pdf合集")
    @SLog(tag = "生成Pdf合集", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateFormSummaryPdfConverge(@Param("sqfwId") String sqfwId, @Param("workOrderId") String workOrderId, @Param("fileType") String fileType) {
        String pdfUrl = sqfwFormSummaryService.generateAppointPdfConverge(sqfwId, workOrderId, fileType);
        if (StringUtils.isBlank(pdfUrl)) return Result.error("该任务下没有排查单");
        return Result.data(pdfUrl);
    }

    @At("/generateAppointPdfConverge")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成指定Pdf合集")
    @SLog(tag = "生成指定Pdf合集", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateAppointPdfConverge(@Param("sqfwId") String sqfwId, @Param("workOrderId") String workOrderId,
                                                @Param("fileType") String fileType, @Param("index") int index) {
        String userId = SecurityUtil.getUserId();
        String pdfUrl = sqfwFormSummaryService.generateAppointPdfConverge(userId, sqfwId, workOrderId, fileType, index);
        if (StringUtils.isBlank(pdfUrl)) return Result.error("该任务下没有排查单");
        return Result.data(pdfUrl);
    }

    //-----------------------------------------------------------------------------------------
    //   垃圾分类调查表相关接口
    //-----------------------------------------------------------------------------------------

    @At("/generateRefuseClassifyPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成垃圾分类PDF")
    @SLog(tag = "生成垃圾分类PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateRefuseClassifyPdf(@Param("..") SqfwFormSummaryWithFlowDTO formSummaryWithFlow) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.generateRefuseClassifyPdf(userId, formSummaryWithFlow);
        return Result.success();
    }

    /**
     * 供小程序使用，无权限，只需要登陆即可
     *
     * @return 返回url
     */
    @At("/generateRefuseClassifyExcelForApp")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成垃圾分类排查表Excel")
    @SLog(tag = "生成垃圾分类排查表Excel", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateRefuseClassifyExcelForApp(@Param("sqfwId") String sqfwId) {
        try {
            String excelUrl = sqfwFormSummaryService.generateRefuseClassifyExcelForApp(sqfwId);
            //if (StringUtils.isBlank(excelUrl)) return Result.error();
            //zhangp 2023-12-14 无数据时返回提示
            if (StringUtils.isBlank(excelUrl)) return Result.error("暂无数据，不能生成Excel");
            return Result.data(excelUrl);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @At("/retryGenerateRefuseClassifyPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "重新生成垃圾分类排查表PDF")
    @SLog(tag = "重新生成垃圾分类排查表PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> retryGenerateRefuseClassifyPdf(@Param("sqfwFlowId") String sqfwFlowId) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.retryGenerateRefuseClassifyPdf(userId, sqfwFlowId);
        return Result.success();
    }

    // ----------------------------------------------------------------------------------------------
    // 生成入户调查表
    // ----------------------------------------------------------------------------------------------
    @At("/listPlaceForHouseSurveyFrom")
    @Ok("json")
    @POST
    @ApiOperation(name = "入户调查查询地点")
    @SLog(tag = "入户调查查询地点", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> listPlaceForHouseSurveyFrom(@Param("unitId") String unitId, @Param("workOrderId") String workOrderId, @Param("a5") String a5, @Param("a4") String a4, @Param("a3") String a3, @Param("a1") String a1) {
        String userId = SecurityUtil.getUserId();
        List<HouseSurveyFormPlaceVO> list = sqfwFormSummaryService.listPlaceForHouseSurveyFrom(unitId, workOrderId, userId, a5, a4, a3, a1);
        return Result.success(list);
    }

    @At("/generateHouseSurveyFormPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成入户调查PDF")
    @SLog(tag = "生成垃圾分类PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateHouseSurveyFormPdf(@Param("..") SqfwFormSummaryWithFlowDTO formSummaryWithFlow) {
        log.info("generateHouseSurveyFormPdf request userId: {}, param : {}", SecurityUtil.getUserId(),
                JSONObject.toJSONString(formSummaryWithFlow));
        sqfwFormSummaryService.generateHouseSurveyFormPdf(formSummaryWithFlow);
        return Result.success();
    }

    /**
     * 供小程序使用，无权限，只需要登陆即可
     *
     * @return 返回url
     */
    @At("/getDownloadHouseSurveyFormExcelType")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取能够生成excel的种类")
    @SLog(tag = "获取能够生成excel的种类", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getDownloadHouseSurveyFormExcelType(@Param("sqfwId") String sqfwId) {
        log.info("getDownloadHouseSurveyFormExcelType info param sqfwId: {}", sqfwId);
        return Result.data(sqfwFormSummaryService.getDownloadHouseSurveyFormExcelType(sqfwId, null));
    }

    /**
     * 供小程序使用，无权限，只需要登陆即可
     *
     * @return 返回url
     */
    @At("/generateHouseSurveyFormExcelForApp")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成入户调查表Excel")
    @SLog(tag = "生成入户调查表Excel", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateHouseSurveyFormExcelForApp(@Param("sqfwId") String sqfwId, @Param("excelType") String excelType) {
        String excelUrl = sqfwFormSummaryService.generateHouseSurveyFormExcelForApp(sqfwId, excelType);
        //if (StringUtils.isBlank(excelUrl)) return Result.error();
        //zhangp 2023-12-14 无数据时返回提示
        if (StringUtils.isBlank(excelUrl)) return Result.error("暂无数据，不能生成Excel");

        return Result.data(excelUrl);
    }

    @At("/retryGenerateHouseSurveyFormPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "重新入户调查表PDF")
    @SLog(tag = "重新入户调查表PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> retryGenerateHouseSurveyFormPdf(@Param("sqfwFlowId") String sqfwFlowId) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.retryGenerateHouseSurveyFormPdf(userId, sqfwFlowId);
        return Result.success();
    }

    @At("/listSecondHouseSurvey")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据服务记录id需要二次入户调查的数据")
    @SLog(tag = "根据服务记录id需要二次入户调查的数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> listSecondHouseSurvey(@Param("sqfwId") String sqfwId) {
        String userId = SecurityUtil.getUserId();
        List<HouseSurveyFormPlaceVO> list = sqfwFormSummaryService.listSecondHouseSurvey(sqfwId, userId);
        return Result.data(list);
    }

    @At("/listSecondHouseSurveyCenPeople")
    @Ok("json")
    @POST
    @ApiOperation(name = "查询二次入户调查的特殊人员数据")
    @SLog(tag = "查询二次入户调查的特殊人员数据", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> listSecondHouseSurveyCenPeople(@Param("..") HouseSurveySecondCenQueryDTO queryDTO) {
        String userId = SecurityUtil.getUserId();
        List<HouseSurveyFormPlaceVO> list = sqfwFormSummaryService.listSecondHouseSurveyCenPeople(queryDTO, userId);
        return Result.data(list);
    }

    @At("/listGridByUnit")
    @Ok("json")
    @POST
    @ApiOperation(name = "查询单位网格")
    @SLog(tag = "查询单位网格", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> listGridByUnit(@Param("unitId") String unitId) {
        List<String> list = sqfwFormSummaryService.listGridByUnit(unitId);
        return Result.data(list);
    }


    //--------------------------------------------------------------------------------------------
    //  根据服务记录id数组合并excel和pdf
    //--------------------------------------------------------------------------------------------
    @At("/generateServiceRecordPdfColl")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据服务记录id数组生成pdf合集")
    @SLog(tag = "根据服务记录id数组生成pdf合集", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateServiceRecordPdfColl(@Param("conditionList") List<ServiceRecordPdfOrExcelCollDTO> conditionList,
                                                  @Param("fileIndex") Integer fileIndex) {
        String pdfUrl = sqfwFormSummaryService.generateServiceRecordPdfColl(conditionList, fileIndex);
        if (StringUtils.isBlank(pdfUrl)) {
            return Result.error("暂无数据");
        }
        return Result.data(pdfUrl);
    }

    @At("/generateServiceRecordExcelColl")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据服务记录id数组生成Excel合集")
    @SLog(tag = "根据服务记录id数组生成Excel合集", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generateServiceRecordExcelColl(@Param("conditionList") List<ServiceRecordPdfOrExcelCollDTO> conditionList, @Param("fileIndex") Integer fileIndex, @Param("excelType") String excelType) {
        String userId = SecurityUtil.getUserId();
        try {
            log.info("generateServiceRecordExcelColl conditionList param: {}", JSONObject.toJSONString(conditionList));
            String excelUrl = sqfwFormSummaryService.generateServiceRecordExcelColl(conditionList, userId, fileIndex, excelType);
            return Result.data(excelUrl);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @At()
    @Ok("json")
    @DELETE
    @ApiOperation(name = "批量删除删除formSummary表数据")
    @SLog(tag = "批量删除删除formSummary表数据", value = "${args[0]}")
    @ApiFormParams({@ApiFormParam(name = "formSummaryIds", description = "ID数组")})
    @SaCheckLogin
    public Result<?> batchDeleteFormSummary(@Param("formSummaryIds") List<String> formSummaryIds) {
        log.info("batchDeleteFormSummary param: {}", formSummaryIds);
        sqfwFormSummaryService.batchDeleteFormSummary(formSummaryIds);
        return Result.success();
    }

    @At()
    @Ok("json")
    @GET
    @ApiOperation(name = "查询formSummary数据（根据id查询）")
    @SaCheckLogin
    public Result<?> listFormSummaryById(@Param("id") String id) {
        return Result.data(sqfwFormSummaryService.fetch(id));
    }

    @At()
    @Ok("json")
    @GET
    @ApiOperation(name = "查询formSummary数据（根据社区服务id查询）")
    @SaCheckLogin
    public Result<?> listFormSummaryBySqfwId(@Param("sqfwId") String sqfwId) {
        List<Sqfw_formSummary> list = sqfwFormSummaryService.query(Cnd.where(Sqfw_formSummary::getSqfwId, "=", sqfwId));
        return Result.data(list);
    }

    /**
     * 根据上传的ExcelType 下载模板
     *
     * @param excelType
     * @return
     * @auth zhangp
     * @Date 2023-11-23
     */
    @At("/getDownloadTemplateFormExcelType")
    @Ok("json")
    @POST
    @ApiOperation(name = "模板下载")
    @SLog(tag = "模板下载", value = "")
    @ApiFormParams({@ApiFormParam(name = "sqfwId", description = "服务ID"), @ApiFormParam(name = "excelType", description = "EXCEL类型")})
    @SaCheckLogin
    public Result<?> getDownloadTemplateFormExcelType(@NotNull @Param("sqfwId") String sqfwId, @NotNull @Param("excelType") String excelType, @Param("fileIndex") Integer fileIndex, @NotNull @Param("isWord") String isWord) {
        log.info("getDownloadTemplateFormExcelType info param:sqfwId:{}, excelType: {},isWord:{}", sqfwId, excelType, isWord);
        return Result.data(sqfwFormSummaryService.getDownloadTemplateFormExcelType(sqfwId, fileIndex, excelType, isWord, null, null));
    }

    /**
     * 数据检测
     * APP端安全检查、垃圾分类、入户调查检查excel、pdf是否有数统一入口
     *
     * @return 返回url
     */
    @At("/getDownloadExcelType")
    @Ok("json")
    @POST
    @ApiOperation(name = "APP端安全检查、垃圾分类、入户调查检查excel是否有数统一入口")
    @SLog(tag = "APP端安全检查、垃圾分类、入户调查检查excel是否有数统一入口", value = "")
    @SaCheckLogin
    public Result<?> getDownloadExcelTypeFromApp(@Param("sqfwId") String sqfwId) {
        log.info("getDownloadExcelType info param sqfwId: {}", sqfwId);
        return Result.data(sqfwFormSummaryService.getDownloadExcelTypeFromApp(sqfwId));
    }

    /**
     * 供APP使用
     *
     * @param formSummaryWithFlow
     * @return
     */
    @At("/generatePublicSecurityPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成社会治安巡查PDF")
    @SLog(tag = "生成社会治安巡查PDF", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generatePublicSecurityPdf(@Param("..") SqfwFormSummaryWithFlowDTO formSummaryWithFlow) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.generatePublicSecurityPdf(userId, formSummaryWithFlow);
        return Result.success();
    }

    /**
     * 重新生成社会治安pdf
     *
     * @param completeflowId
     * @return
     */
    @At("/retryGeneratePublicSecurityPdf")
    @Ok("json")
    @POST
    @ApiOperation(name = "重新生成社会治安pdf")
    @SLog(tag = "重新生成社会治安pdf", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> retryGeneratePublicSecurityPdf(@Param("completeflowId") String completeflowId) {
        String userId = SecurityUtil.getUserId();
        sqfwFormSummaryService.retryGeneratePublicSecurityPdf(userId, completeflowId);
        return Result.success();
    }

    /**
     * 供app使用，无权限，只需要登陆即可
     *
     * @return 返回url
     */
    @At("/generatePublicSecurityExcelForApp")
    @Ok("json")
    @POST
    @ApiOperation(name = "生成社会治安表Excel合集")
    @SLog(tag = "生成社会治安表Excel合集", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> generatePublicSecurityExcelForApp(@Param("sqfwId") String sqfwId, @Param("workOrderId") String workOrderId, @Param("fileIndex") Integer fileIndex) {
        String userId = SecurityUtil.getUserId();
        try {
            String excelUrl = sqfwFormSummaryService.generatePublicSecurityExcelForApp(sqfwId, workOrderId, userId, fileIndex);
            if (StringUtils.isBlank(excelUrl)) return Result.error("暂无数据，不能生成Excel");
            return Result.data(excelUrl);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 根据社会治安场所名称查询历史
     *
     * @param placeName
     * @return
     */
    @At("/queryPlaceNameHistory")
    @Ok("json")
    @GET
    @ApiOperation(name = "查询社会治安场所名称历史数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "placeName", description = "场所名称", required = true, check = true)
    })
    @SaCheckLogin
    public Result<?> queryPlaceNameHistory(@Param("placeName") @NotNull String placeName) {
        log.info("SqfwFormSummaryController queryPlaceNameHistory param: {}", placeName);
        return Result.data(sqfwFormSummaryService.queryPlaceNameHistory(placeName));
    }

    /**
     * 根据条件检索场所名称（上级可查看下面子级信息）(接口暂时不用)
     * @param unitId
     * @param type
     * @param checkPlaceNameStr
     * @param returnKey
     * @return
     */

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据条件检索场所名称（上级可查看下面子级信息）")
    @ApiResponses
    @SLog(tag = "根据条件检索场所名称（上级可查看下面子级信息）", value = "${args[0]},${args[1]}")
    @SaCheckLogin
    public Result<?> queryPlaceInfoList(@Param("unitId") String unitId, @Param("type") String type, @Param("unitName") String checkPlaceNameStr, @Param("returnKey") String returnKey) {
        List<Map> list = sqfwFormSummaryService.queryPlaceInfoList(unitId, type, checkPlaceNameStr, returnKey);
        return Result.success(list);
    }

    /**
     * 根据场所名称获取最新场所信息(上级可检索下级)(接口暂时不用)
     * @param unitNameStr
     * @param unitId
     * @return
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据场所名称获取最新场所信息(上级可检索下级)")
    @ApiResponses
    @SLog(tag = "根据场所名称获取最新场所信息(上级可检索下级)", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getUnitPlaceLatestInfoByName(@Param("unitName") String unitNameStr, @Param("unitId") String unitId, @Param("conditionKey") String conditionKey,@Param("type") String type) {
        SqfwFormSummaryUnitInfoVO vo = sqfwFormSummaryService.getUnitPlaceLatestInfoByName(unitNameStr, unitId,conditionKey,type);
        return Result.success(vo);
    }

    /**
     * 根据当前人员查询寻访记录
     * @return
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据当前人员查询寻访记录")
    @ApiResponses
    @SLog(tag = "根据当前人员查询寻访记录", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getServiceRecordFormSummaryList(@Param("userName") String userName, @Param("specialMark") String specialMark) {
        return Result.data(sqfwFormSummaryService.getServiceRecordFormSummaryList(userName, specialMark));
    }


    /**
     * 根据选择的任务合并文件下载
     * @return
     */
    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "根据选择的任务合并文件下载")
    @ApiResponses
    @SLog(tag = "根据选择的任务合并文件下载", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> downloadFileMerging(@Param("..") ServerFileMergingDTO serverFileMergingDTO) {
        return sqfwFormSummaryService.downloadFileMerging(serverFileMergingDTO);
    }


}
